const express = require('express')
const bodyParser = require('body-parser')
// const logger = require('morgan')
const { Nuxt, Builder } = require('nuxt')
const app = express()
const config = require('../nuxt.config.js')
const apiInt = require('./api')
const sequelize = require('./mysql')
config.dev = process.env.NODE_ENV === 'development'
/* 接收post参数 */
app.use(bodyParser.json())
// app.use(logger('dev'))
apiInt(app)

async function start() {
  // Init Nuxt.js
  const nuxt = new Nuxt(config)
  const { host, port } = nuxt.options.server
  // Build only in dev mode
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  } else {
    await nuxt.ready()
  }
  // Give nuxt middleware to express
  app.use(nuxt.render)
  // Listen the server
  // api(app)
  await sequelize.sync({ force: false })
  app.listen(port, host)
}

start()
